﻿using System;
using System.Collections.Generic;
using System.Globalization;
using System.Windows;
using System.Windows.Controls;

namespace Notoric.Gui.Windows
{
    /// <summary>
    /// Interaction logic for ChangeKeySignatureDialog.xaml
    /// </summary>
    public partial class ChangeKeySignatureDialog : Window
    {
        #region Constructors

        /// <summary>
        /// Initializes a new instance of the <see cref="ChangeKeySignatureDialog"/> class.
        /// </summary>
        public ChangeKeySignatureDialog()
        {
            selectedParts = new List<ViewModel.Elements.PartDescriptor>();
            InitializeComponent();
        }

        #endregion

        #region Event Handlers

        /// <summary>
        /// Handles the Checked event of the CheckBox control.
        /// </summary>
        /// <param name="sender">The source of the event.</param>
        /// <param name="e">The <see cref="System.Windows.RoutedEventArgs"/> instance containing the event data.</param>
        private void CheckBox_Checked(object sender, RoutedEventArgs e)
        {
            selectedParts.Add((ViewModel.Elements.PartDescriptor)PART_SelectedParts.SelectedItem);
            e.Handled = true;
        }

        /// <summary>
        /// Handles the Unchecked event of the CheckBox control.
        /// </summary>
        /// <param name="sender">The source of the event.</param>
        /// <param name="e">The <see cref="System.Windows.RoutedEventArgs"/> instance containing the event data.</param>
        private void CheckBox_Unchecked(object sender, RoutedEventArgs e)
        {
            selectedParts.Remove((ViewModel.Elements.PartDescriptor)PART_SelectedParts.SelectedItem);
            e.Handled = true;
        }

        /// <summary>
        /// Handles the Checked event of the PART_SharpsFlats control.
        /// </summary>
        /// <param name="sender">The source of the event.</param>
        /// <param name="e">The <see cref="System.Windows.RoutedEventArgs"/> instance containing the event data.</param>
        private void PART_SharpsFlats_Checked(object sender, RoutedEventArgs e)
        {
            PART_Key.SelectedIndex = 0;
        }

        /// <summary>
        /// Handles the Click event of the PART_OK control.
        /// </summary>
        /// <param name="sender">The source of the event.</param>
        /// <param name="e">The <see cref="System.Windows.RoutedEventArgs"/> instance containing the event data.</param>
        private void PART_OK_Click(object sender, RoutedEventArgs e)
        {
            if (selectedParts.Count > 0)
            {
                var fromMeasure = (ushort)Math.Max(PART_From.Minimum, Math.Min(PART_From.Value, PART_From.Maximum));
                var toMeasure = (ushort)Math.Max(PART_To.Minimum, Math.Min(PART_To.Value, PART_To.Maximum));
                var keyStrings = ((ComboBoxItem)PART_Key.SelectedItem).Tag.ToString().Split(',');
                var numberFormat = new CultureInfo("en-US").NumberFormat;
                var key = new double[7];
                for (int i = 0; i < 7; ++i)
                    key[i] = double.Parse(keyStrings[i], numberFormat);

                var composition = (ViewModel.Elements.Composition)DataContext;
                var cmd = new ViewModel.ChangeKeyCommand(composition.UndoStack, composition.RedoStack)
                {
                    FromMeasure = fromMeasure,
                    Key = key,
                    Parts = selectedParts,
                    ToMeasure = toMeasure
                };
                cmd.Execute(null);
            }

            this.DialogResult = true;
        }

        /// <summary>
        /// Handles the Click event of the PART_Cancel control.
        /// </summary>
        /// <param name="sender">The source of the event.</param>
        /// <param name="e">The <see cref="System.Windows.RoutedEventArgs"/> instance containing the event data.</param>
        private void PART_Cancel_Click(object sender, RoutedEventArgs e)
        {
            this.DialogResult = false;
        }

        /// <summary>
        /// Handles the DataContextChanged event of the window.
        /// </summary>
        /// <param name="sender">The source of the event.</param>
        /// <param name="e">The <see cref="System.Windows.DependencyPropertyChangedEventArgs"/> instance containing the event data.</param>
        private void Window_DataContextChanged(object sender, DependencyPropertyChangedEventArgs e)
        {
            var composition = DataContext as ViewModel.Elements.Composition;

            if (composition != null)
            {
                selectedParts.Clear();
                selectedParts.AddRange(composition.Parts);
            }
        }

        #endregion

        #region Private Fields

        /// <summary>
        /// Holds the list of selected parts.
        /// </summary>
        private List<ViewModel.Elements.PartDescriptor> selectedParts;

        #endregion
    }
}
